package com.example.fmgidsprototype;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.fmgidsprototype.model.Review;

public class ReviewAdapter extends ArrayAdapter<Review>{
	
	private Context ctx;

	public ReviewAdapter(Context context, List<Review> reviews) {
		super(context, R.layout.review_list_item, reviews);
		this.ctx = context;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		Review review = this.getItem(position);
		
		if(rowView == null){
			LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = inflater.inflate(R.layout.review_list_item, parent, false);
			TextView recensieText = (TextView)rowView.findViewById(R.id.detail_recensieTxt);
			TextView recensieMeta = (TextView)rowView.findViewById(R.id.detail_recensieMeta);
			ViewHolder newHolder = new ViewHolder();
			newHolder.recensieText = recensieText;
			newHolder.recensieMeta = recensieMeta;
			rowView.setTag(newHolder);
		}
		ViewHolder holder = (ViewHolder)rowView.getTag();
		holder.recensieText.setText(review.getComment());
		String dateString = new SimpleDateFormat("dd/MM/yyyy").format(new Date(review.getTimestamp() * 1000));
		holder.recensieMeta.setText("- " + dateString + ", " + review.getUserName());
		return rowView;
	}
	
	protected static class ViewHolder{
		protected TextView recensieText;
		protected TextView recensieMeta;
	}

}
